﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Share.Data;
using System.ComponentModel;
using Share.Utilities;


namespace ActiveOnline
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class WinMainServer : Window
    {
        public WinMainServer()
        {
            InitializeComponent();
            Grid_Root.DataContext = _WinMainServer_data;
            //Grid_ClientMessageDetail.DataContext = _WinMainServer_data.ClientMessageSelected;
            SetUpEvent();

        }
        void SetUpEvent()
        {
            Button_Add.Click -= new RoutedEventHandler(Button_Add_Click);
            Button_Add.Click += new RoutedEventHandler(Button_Add_Click);

            Button_Create.Click -= new RoutedEventHandler(Button_Create_Click);
            Button_Create.Click += new RoutedEventHandler(Button_Create_Click);

            Button_Save.Click -= new RoutedEventHandler(Button_Save_Click);
            Button_Save.Click += new RoutedEventHandler(Button_Save_Click);

            Button_Delete.Click -= new RoutedEventHandler(Button_Delete_Click);
            Button_Delete.Click += new RoutedEventHandler(Button_Delete_Click);

            Button_Close.Click -= new RoutedEventHandler(Button_Close_Click);
            Button_Close.Click += new RoutedEventHandler(Button_Close_Click);
        }

        void Button_Add_Click(object sender, RoutedEventArgs e)
        {
           string notice = _WinMainServer_data.AddClientMessage(_WinMainServer_data.ClientMessageSelected);
           MessageBox.Show(notice);
           _WinMainServer_data.Refresh();
        }
        void Button_Delete_Click(object sender, RoutedEventArgs e)
        {
            string notice = _WinMainServer_data.DeletedClientMessageSelected(_WinMainServer_data.ClientMessageSelected);
            MessageBox.Show(notice);
            _WinMainServer_data.Refresh();
        }
        void Button_Close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        void Button_Create_Click(object sender, RoutedEventArgs e)
        {
            _WinMainServer_data.CreateNewClientMessage();
            //Grid_ClientMessageDetail.DataContext = _WinMainServer_data.ClientMessageSelected;
        }
        void Button_Save_Click(object sender, RoutedEventArgs e)
        {
           string notice = _WinMainServer_data.Save();
           MessageBox.Show(notice);
           _WinMainServer_data.Refresh();
        }
        WinMainServer_data _WinMainServer_data = new WinMainServer_data();
    }
    public class WinMainServer_data : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged Members
        public void SendPropertyChanged(string s)
        {
            if (this.PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(s));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Clients
        Share.Data.Client[] _clients;
        public Share.Data.Client[] Clients
        {
            get
            {
                if (_clients == null)
                {
                    _clients = DC.Clients.Where(c => c.IsDeleted == false).ToArray();
                }
                return _clients;
            }
            set
            {
                if (_clients != value)
                {
                    _clients = value;
                    SendPropertyChanged("Clients");
                }
            }
        }
        
        #endregion

        #region ClientMessages

        #region _clientMessages
        Share.Data.ClientMessage[] _clientMessages;
        public Share.Data.ClientMessage[] ClientMessages
        {
            get
            {
                if (_clientMessages == null)
                {
                    _clientMessages = DC.ClientMessages.Where(c => c.IsDeleted == false).ToArray();
                }
                return _clientMessages;
            }
            set
            {
                if (_clientMessages != value)
                {
                    _clientMessages = value;
                    SendPropertyChanged("ClientMessages");
                }
            }
        }
        #endregion

        #region add clientMessage

        public string AddClientMessage(ClientMessage _clientMess)
        {
            try
            {
                if (_clientMess == null)
                    return "Message is null";
                ClientMessage _cM = new ClientMessage();
                #region Copy
                var client = Clients.FirstOrDefault(c => c.ClientId == _clientMess.ClientId);
                _cM.AtTime = _clientMess.AtTime;
                _cM.ClientId = _clientMess.ClientId;
                _cM.ClientCode = SysInfo.HDDNumID;
                _cM.ClientCodeOfClient = client == null ? string.Empty : client.ClientCode;
                _cM.ComputerName = client == null ? "All Client" : client.ComputerName;
                _cM.CreatedDate = DateTime.Now;
                _cM.Message = _clientMess.Message;
                _cM.SyncCode = string.Empty;
                var _clientMessOld = ClientMessages.FirstOrDefault(c => c.ClientId == _cM.ClientId && c.AtTime.Date == _cM.AtTime.Date);
                if (_clientMessOld != null)
                    return "Messeage for this Client at this time has been existed";
                #endregion
                DC.ClientMessages.InsertOnSubmit(_cM);
                DC.SubmitChanges();
                SendPropertyChanged("ClientMessages");
                return "Insert Message Success";
            }
            catch(Exception ex)
            {
                return ex.Message;
            }
        }
        #endregion

        #region ClientMessage Selected
        Share.Data.ClientMessage _clientMessageSelected;
        public Share.Data.ClientMessage ClientMessageSelected
        {
            get
            {
                if (_clientMessageSelected == null)
                    _clientMessageSelected = new ClientMessage(); 
                return _clientMessageSelected;
            }
            set
            {
                if (_clientMessageSelected != value)
                {
                    _clientMessageSelected = value;
                    ClientMessageSelected.PropertyChanged -= new PropertyChangedEventHandler(ClientMessageSelected_PropertyChanged);
                    ClientMessageSelected.PropertyChanged += new PropertyChangedEventHandler(ClientMessageSelected_PropertyChanged);
                    SendPropertyChanged("ClientMessageSelected");
                }
            }
        }

        void ClientMessageSelected_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            ClientMessage _cM = sender as ClientMessage;
            if (_cM != null)
            {
                if (e.PropertyName == "ClientId")
                {
                    var _client = Clients.FirstOrDefault(c=>c.ClientId == _cM.ClientId);
                    ClientMessageSelected.ClientCodeOfClient = _client == null ? string.Empty : _client.ClientCode;
                }
            }
        }

        #endregion
        #region DeletedClientMessageSelected
        public string DeletedClientMessageSelected(ClientMessage _clientMess)
        {
            try
            {
                if (_clientMess == null)
                    return "Message is null";
                var _cM = DC.ClientMessages.FirstOrDefault(c => c.ClientMessageId == _clientMess.ClientMessageId);
                if (_cM != null)
                {
                    _cM.IsDeleted = true;
                    DC.SubmitChanges();

                }
                else
                {
                    return "Message has not existed";
                }
                return "Deleted success";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        #endregion
        #endregion

        #region DC
        DataClientDataContext _dc;
        public DataClientDataContext DC
        {
            get
            {
                if (_dc == null)
                    _dc = ShareDataCache.GetDataClientDataContext();
                return _dc;
            }
        }
        #endregion

        #region Save
        public string Save()
        {
            try
            {
                var change =DC.GetChangeSet();
                DC.SubmitChanges();
                
                return "Save Success";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public void CreateNewClientMessage()
        {
            ClientMessageSelected = new ClientMessage();
            ClientMessageSelected.AtTime = DateTime.Now;
            
            ClientMessageSelected.PropertyChanged -= new PropertyChangedEventHandler(ClientMessageSelected_PropertyChanged);
            ClientMessageSelected.PropertyChanged += new PropertyChangedEventHandler(ClientMessageSelected_PropertyChanged);

        }
        #endregion

        #region Refresh
        public void Refresh()
        {
            _dc = null;
            _clients = null;
            _clientMessages = null;
            SendPropertyChanged("ClientMessages");
            SendPropertyChanged("Clients");
        }
        #endregion



    }
}
